package gui;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import common.ChatIF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
/**
 * 
 * @author Chen Parodi
 * This GUI Class services the Assessors,
 * With it they are able to set the number of days needed for analyzing a request.
 *  *
 */
public class SetAssessDuration extends SetTaskDuration implements ChatIF{
	String newStatus;
	JComboBox<String> assessorRequestsComboBox;
	private JTextField daysTextField;
	JButton btnSubmit;
	
	public SetAssessDuration(AcademicFrame frame,AssessorPanel assessor, String pid){
		super(frame, assessor, pid);
		initialize();
		}
	private void initialize(){
		
		daysTextField = new JTextField();
		daysTextField.setBounds(271, 170, 86, 20);
		add(daysTextField);
		daysTextField.setColumns(10);
		
		btnSubmit = new JButton("Set duration");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					String days= daysTextField.getText();
					Integer durtion= Integer.parseInt(days);
					newStatus="3";
					String request=assessorRequestsComboBox.getSelectedItem().toString();		
					message= new ArrayList<Object>();
					message.add("setTaskDuration");
					message.add(request);
					message.add(days);
					message.add(newStatus);
					
					AcademicFrame.myclient.sendMessageToServer(message);
				}
				catch(NumberFormatException ex){
					(new Dialog ("The 'Number of days' field excepts only numbers")).setVisible(true);
					daysTextField.setText("");
				}
			}
		});
		btnSubmit.setBounds(298, 253, 130, 23);
		add(btnSubmit);
		
		
		assessorRequestsComboBox = new JComboBox<String>();
		assessorRequestsComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!assessorRequestsComboBox.getSelectedItem().toString().equalsIgnoreCase("--Choose a Request--")){
					btnSubmit.setEnabled(true);
				}
				else{
					btnSubmit.setEnabled(false);
				}
			}
		});
		assessorRequestsComboBox.setBounds(120, 112, 199, 20);
		add(assessorRequestsComboBox);
	}
	public void display(Object message)
	{
		ArrayList<Object> listofAssessorRequests=new ArrayList<Object>();
		ArrayList<Object> TaskDuration=new ArrayList<Object>();
		listofAssessorRequests.clear(); // clear the info array list
		TaskDuration.clear(); // clear the info array list
		listofAssessorRequests = (ArrayList<Object>)message;
		TaskDuration = (ArrayList<Object>)message;
		
		
		if (((String)(listofAssessorRequests.get(0))).equalsIgnoreCase("listOfExRequests"))
		{
			ArrayList<String> assessorRequests = (ArrayList<String>)message; 
									
			int numberOfSystems = Integer.parseInt((assessorRequests.get(1)).toString()); // get the number of systems in the list
			this.assessorRequestsComboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.assessorRequestsComboBox.addItem((String)assessorRequests.get(i+2));  // add systems to the list
			}
		}	
		else if (((String)(TaskDuration.get(0))).equalsIgnoreCase("TaskDuration"))
		{
			
			ArrayList<String> replyFromServer = (ArrayList<String>)message; 
										
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("TaskDurationSet") )
			{
			
				(new Dialog ("The assessment duration was set, thank you.")).setVisible(true);
				
				frame.setSize(500, 500);
				frame.setContentPane(panel);	
			}
			else
			{
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
			
				frame.setSize(500, 500);
				frame.setContentPane(panel);	
			}
		}			
	}
}
